#include "../server.hpp"

class EchoServer
{
private:
    TcpServer _svr;

private:
    void OnConnected(const ConnectionPtr &conn)
    {
        lg(Info, "NEW CONNECTION: %p, id:%d", conn.get(), conn->Id());
    }

    void OnMessage(const ConnectionPtr &conn, Buffer *buf)
    {
        conn->Send(buf->ReadPosition(), buf->ReadAbleSize());
        buf->MoveReaderOffset(buf->ReadAbleSize());
        conn->Shutdown();
    }

    void OnClose(const ConnectionPtr &conn)
    {
        lg(Info, "CLOSE CONNECTION: %p, id:%d", conn.get(), conn->Id());
    }

public:
    EchoServer(int port) : _svr(port)
    {
        _svr.SetThreadcount(2);
        _svr.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this,  std::placeholders::_1));
        _svr.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
        _svr.SetCloseCallback(std::bind(&EchoServer::OnClose, this, std::placeholders::_1));
        _svr.EnableInactiveRelease(10);
    }
    void Start() { _svr.Start(); }
};